//
//  SEExamineRecordViewModel.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/25.
//

import UIKit
import HandyJSON

class SEExamineRecordViewModel: NSObject {
    var recordModels: [SEExamineRecordModel] = []

    var heathModel = SEPatientFileHealthModel()

    var updateData: ((SEHttpStatus) -> Void)?

    var imageDTOList: [SEUploadFileModel] {
        var models: [SEUploadFileModel] = []
        recordModels.forEach{
            models.append(contentsOf: $0.imageDTOList)
        }
        return models
    }

    var resultDTODescription: String {
        return visionResult + refractiveResult + eyePressureResult
    }

    func filterResultViewModel<T: SEExamineRecordBaseViewModel>(_ checkItemCode: Int, _ type: T.Type) -> T? {
        let models = recordModels.filter {
            $0.checkItemCode == checkItemCode && $0.dataDTO.last?.count == 2
        }
        guard let model = models.first else { return nil }

        return T.init(model: model)
    }

    var eyePressureResult: String {
        guard let vm = filterResultViewModel(103, SEExamineRecordEyePressureViewModel.self) else { return "" }
        return vm.resultDescription
    }

    var visionResult: String {
        guard let vm = filterResultViewModel(104, SEExamineRecordVisionViewModel.self) else { return "" }
        return vm.resultDescription
    }

    var refractiveResult: String {
        guard let vm = filterResultViewModel(105, SEExamineRecordRefractiveViewModel.self) else { return "" }
        return vm.resultDescription
    }


    // MARK: - APIRequest

    func requestExamineRecord() {
        SEHttpManager.shared.request(SEExamineAPI.examineRecord(applyFormId: heathModel.checkData.applyFormId), model: [SEExamineRecordModel].self, withLoading: true) { (models, status) in
            switch status {
            case .success:
                self.recordModels = models ?? []
            default: break
            }
            self.updateData?(status)
        }
    }
}
